package cn.edu.zjicm.wordsnet_d.activity;

import java.util.Random;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.base.WordBaseActivity;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;
import cn.edu.zjicm.wordsnet_d.util.NightModeUtil;
import cn.edu.zjicm.wordsnet_d.util.StringUtil;
import cn.edu.zjicm.wordsnet_d.view.ExperienceNumRegularView;
import cn.edu.zjicm.wordsnet_d.view.OnRiseCompleteListener;
import cn.edu.zjicm.wordsnet_d.view.RiseNumTextView;

public class ShowRegularActivity extends WordBaseActivity implements OnRiseCompleteListener {

	public static final int TYPE_EXPERIENCE_NUM = 0;
	public static final int TYPE_PUNCH_OUT = 1;
	public static final int TYPE_EXPERIENCE_NUM2 = 2; // 个人账号点击进入时
	public static final int TYPE_LEADER_BOARD = 3;
	public static final int TYPE_BALANCE = 4;
	public static final int TYPE_JUNIOR_PHRASE_INFO_2 = 6; // 《中考必备单词与词组》
	public static final int TYPE_SENIOR_PHRASE_INFO_2 = 8; // 《高考必备单词与词组》
	public static final int TYPE_CET4_INFO = 5; // 四级单词+词组
	public static final int TYPE_CET6_INFO = 7; // 六级单词+词组
	public static final int TYPE_KAOYAN_INFO_1 = 9; // 考研补充词组
	public static final int TYPE_KAOYAN_INFO_2 = 10; // 考研单词+词组
	public static final int TYPE_IELTS_INFO = 11; // 雅思单词+词组
	public static final int TYPE_TOEFL_INFO = 12; // 托福单词+词组

	private RiseNumTextView experienceNum;
	private ExperienceNumRegularView experienceNumRegularView;
	private RelativeLayout showExperienceNumLayout;
	private ImageView experienceImg;
	private TextView zhimiBeanTv;
	private LinearLayout regularLayout;
	private int showType;
	private int exp;
	private int initValue = 0;
	private int balance = 0;

	public static void startActivity(Context context, int showType) {
		Intent in = new Intent(context, ShowRegularActivity.class);
		in.putExtra("showType", showType);
		context.startActivity(in);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show_regular);
		setTitleBarStyle(TITLE_BAR_STYLE_BACK_BUTTON);
		experienceNumRegularView = (ExperienceNumRegularView) findViewById(R.id.experienceNumView);
		experienceNum = (RiseNumTextView) findViewById(R.id.experienceNumTv);
		showExperienceNumLayout = (RelativeLayout) findViewById(R.id.showExperienceNumLayout);
		regularLayout = (LinearLayout) findViewById(R.id.regular_container);

		showType = getIntent().getIntExtra("showType", TYPE_EXPERIENCE_NUM);
		if (showType != TYPE_EXPERIENCE_NUM2 && showType != TYPE_BALANCE) {
			overridePendingTransition(R.anim.zoom_in, 0);
		}

		setBackButton(new OnClickListener() {

			@Override
			public void onClick(View v) {
				toFinish();
			}
		});

		switch (showType) {
		case TYPE_EXPERIENCE_NUM:
			showExperienceNumRegular();
			break;

		case TYPE_PUNCH_OUT:
			showPunchOutRegular();
			break;

		case TYPE_EXPERIENCE_NUM2:
			showExperienceNumRegular2();
			break;

		case TYPE_LEADER_BOARD:
			showLeaderboardRegular();
			break;

		case TYPE_BALANCE:
			showBalanceRegular();
			break;

		case TYPE_JUNIOR_PHRASE_INFO_2:
			showBookInfo(getResources().getString(R.string.junior_info_2));
			break;

		case TYPE_SENIOR_PHRASE_INFO_2:
			showBookInfo(getResources().getString(R.string.senior_info_2));
			break;
			
		case TYPE_CET4_INFO:
			showBookInfo(getResources().getString(R.string.cet4_info));
			break;
			
		case TYPE_CET6_INFO:
			showBookInfo(getResources().getString(R.string.cet6_info));
			break;
			
		case TYPE_KAOYAN_INFO_1:
			showBookInfo(getResources().getString(R.string.kaoyan_info_1));
			break;
			
		case TYPE_KAOYAN_INFO_2:
			showBookInfo(getResources().getString(R.string.kaoyan_info_2));
			break;
			
		case TYPE_IELTS_INFO:
			showBookInfo(getResources().getString(R.string.ielts_info));
			break;
			
		case TYPE_TOEFL_INFO:
			showBookInfo(getResources().getString(R.string.toefl_info));
			break;
		}

	}

	private void showBookInfo(String q1) {
		setTitleText("词组说明");
		addRegular(q1, null);
		findViewById(R.id.regular_hint).setVisibility(View.VISIBLE);
	}

	private void showBalanceRegular() {
		setTitleText("知米豆");
		showExperienceNumLayout.setVisibility(View.VISIBLE);
		if (NightModeUtil.isNightModeNow()) {
			showExperienceNumLayout.setBackgroundColor(getResources().getColor(R.color.regular_bg));
		} else {
			showExperienceNumLayout.setBackgroundResource(R.drawable.regular_bg);
		}

		experienceImg = (ImageView) findViewById(R.id.experience_logo);
		experienceImg.setVisibility(View.GONE);
		zhimiBeanTv = (TextView) findViewById(R.id.zhimi_bean);
		zhimiBeanTv.setVisibility(View.VISIBLE);

		balance = WordFactory.getInstance(this).getZMD();
		experienceNum.setValue(balance, this);
		experienceNum.startRise();

		addRegular(getResources().getString(R.string.ZM_Q1), getResources().getString(R.string.ZM_A1));
		addRegular(getResources().getString(R.string.ZM_Q2), getResources().getString(R.string.ZM_A2));
		addRegular(getResources().getString(R.string.ZM_Q3), getResources().getString(R.string.ZM_A3));

		findViewById(R.id.regular_hint).setVisibility(View.VISIBLE);

	}

	private void showLeaderboardRegular() {
		setTitleText("校园排行榜规则");
		addRegular(getResources().getString(R.string.leaderboard_regular1), getResources().getString(R.string.leaderboard_regular2));
		addRegular(getResources().getString(R.string.leaderboard_regular3), getResources().getString(R.string.leaderboard_regular4));
	}

	// 从左侧菜单栏进入
	private void showExperienceNumRegular2() {
		setTitleText("经验值");
		showExperienceNumLayout.setVisibility(View.VISIBLE);
		if (NightModeUtil.isNightModeNow()) {
			showExperienceNumLayout.setBackgroundColor(getResources().getColor(R.color.regular_bg));
		} else {
			showExperienceNumLayout.setBackgroundResource(R.drawable.regular_bg);
		}

		addRegular(getResources().getString(R.string.experience_num_regular), null);
		addRegular(getResources().getString(R.string.experience_num_regular1), null);

		experienceNumRegularView.setVisibility(View.VISIBLE);
		exp = AppPreference.getEXP(this);
		experienceNum.setValue(exp, this);
		experienceNum.startRise();
	}

	private void showPunchOutRegular() {
		setTitleText("什么是打卡");
		addRegular(getResources().getString(R.string.punch_out_regular), null);
		findViewById(R.id.divider).setVisibility(View.VISIBLE);
		findViewById(R.id.punch_regular_layout).setVisibility(View.VISIBLE);
	}

	private void showExperienceNumRegular() {
		setTitleText("什么是经验值");
		addRegular(getResources().getString(R.string.experience_num_regular), null);
		addRegular(getResources().getString(R.string.experience_num_regular1), null);
		experienceNumRegularView.setVisibility(View.VISIBLE);
	}

	private void toFinish() {
		finish();
		if (showType != TYPE_EXPERIENCE_NUM2 && showType != TYPE_BALANCE) {
			overridePendingTransition(0, R.anim.zoom_out);
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			toFinish();
		}
		return true;
	}

	private void startAnimation2() {
		experienceNum.clearAnimation();
		Animation animation = AnimationUtils.loadAnimation(this, R.anim.num_animation);
		experienceNum.setAnimation(animation);
		animation.start();
	}

	int getRandomNum(int max) {
		Random random = new Random();
		return random.nextInt(max);
	}

	final int UPDATE = 0;

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case UPDATE:
				experienceNum.setText("" + initValue);
				break;
			}
		};
	};

	@Override
	public void onRiseCompleted() {
		startAnimation2();
	}

	private void addRegular(String question, String answer) {
		View view = LayoutInflater.from(this).inflate(R.layout.view_regular_item, null);
		TextView questionTv = (TextView) view.findViewById(R.id.regular_tv);
		TextView answerTv = (TextView) view.findViewById(R.id.regular_tv_small);
		if (question != null) {
			questionTv.setText(question);
		} else {
			questionTv.setVisibility(View.GONE);
		}
		if (answer != null) {
			answerTv.setText(answer);
		} else {
			answerTv.setVisibility(View.GONE);
		}
		regularLayout.addView(view);
	}
}
